#!/usr/bin/env python3
"""
初始化翻译数据脚本
运行: python seed_data.py
"""

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from main import Translation, DATABASE_URL

# 创建数据库连接
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 初始化数据
initial_translations = [
    {"key": "menu.file", "english": "File", "chinese": "文件"},
    {"key": "menu.edit", "english": "Edit", "chinese": "编辑"},
    {"key": "menu.tools", "english": "Tools", "chinese": "工具"},
    {"key": "menu.help", "english": "Help", "chinese": "帮助"},
    {"key": "button.save", "english": "Save", "chinese": "保存"},
    {"key": "button.load", "english": "Load", "chinese": "加载"},
    {"key": "button.export", "english": "Export", "chinese": "导出"},
    {"key": "label.pokemon", "english": "Pokémon", "chinese": "宝可梦"},
    {"key": "label.trainer", "english": "Trainer", "chinese": "训练家"},
    {"key": "label.level", "english": "Level", "chinese": "等级"},
    {"key": "label.nature", "english": "Nature", "chinese": "性格"},
    {"key": "label.ability", "english": "Ability", "chinese": "特性"},
    {"key": "label.moves", "english": "Moves", "chinese": "招式"},
    {"key": "label.stats", "english": "Stats", "chinese": "能力值"},
    {"key": "label.iv", "english": "IV", "chinese": "个体值"},
    {"key": "label.ev", "english": "EV", "chinese": "努力值"},
    {"key": "error.invalid_file", "english": "Invalid file format", "chinese": "文件格式无效"},
    {"key": "success.saved", "english": "Data saved successfully", "chinese": "数据保存成功"},
    {"key": "warning.unsaved", "english": "You have unsaved changes", "chinese": "您有未保存的更改"},
]

def init_database():
    """初始化数据库并插入示例数据"""
    session = SessionLocal()
    
    try:
        # 清空现有数据
        session.query(Translation).delete()
        
        # 插入新数据
        for trans in initial_translations:
            translation = Translation(**trans)
            session.add(translation)
        
        session.commit()
        print("✅ 数据库初始化完成，已插入示例翻译数据")
        
        # 查询并显示数据
        translations = session.query(Translation).all()
        print(f"📊 共插入 {len(translations)} 条翻译数据")
        
    except Exception as e:
        session.rollback()
        print(f"❌ 初始化失败: {e}")
    finally:
        session.close()

if __name__ == "__main__":
    init_database()
